package cn.hfatec.shms.system.user.service;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.exception.KeyGenException;
import cn.hfatec.shms.system.user.dao.UserDao;
import cn.hfatec.shms.system.user.model.User;
import cn.hfatec.shms.system.user.model.UserParams;
import cn.hfatec.shms.system.userRole.dao.UserRoleDao;
import com.google.common.base.Optional;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


/**
 * @author hfatec
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Service("UserService")
public class UserService {

    //注入Dao层
    @Resource(name = "UserDao")
    private UserDao userDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    @Resource(name = "UserRoleDao")
    private UserRoleDao userRoleDao;


    /**
     * 查询数据
     *
     * @param userParams
     * @param page
     */
    @Transactional
    @MysqlDataSource
    public Table queryUserTable(UserParams userParams, Page page) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", userParams);
        return baseDao.queryTableByPage("queryUser", params, page);
    }
    /**
     * 查询数据
     *
     * @param userParams
     * @param page
     */
    @Transactional
    @MysqlDataSource
    public Table queryUserTables(UserParams userParams, Page page) {

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", userParams);
        return baseDao.queryTableByPage("selectUser", params, page);
    }
    /**
     * 查询数据
     *
     * @param userParams
     */
    @Transactional
    @MysqlDataSource
    public List queryUserList(UserParams userParams) {

        return userDao.queryUser(userParams);
    }

    /**
     * 插入数据
     *
     * @param user
     */
    @Transactional
    @MysqlDataSource
    public void saveUser(User user) throws KeyGenException {
        if (user.getId() != null) {
            User result = getUser(user.getId());
            if (result != null) {

                userDao.update(user);
            } else {
                user.setPassword(DigestUtils.md5Hex(user.getPassword()));
                userDao.insert(user);
            }
        } else {
            user.setPassword(DigestUtils.md5Hex(user.getPassword()));
            userDao.insert(user);
        }
    }

    /**
     * 删除数据
     *
     * @param id
     */
    @Transactional
    @MysqlDataSource
    public void deleteUser(Integer id) throws KeyGenException {
        User user = getUser(id);
        userRoleDao.deleteUserRoleBatch(user.getLoginName());
        userDao.delete(id);
    }

    /**
     * 删除数据
     *
     * @param userList
     */
    @Transactional
    @MysqlDataSource
    public void deleteUserBatch(List<String> userList) throws KeyGenException {
        for (String id : userList) {
            deleteUser(Integer.valueOf(id));
        }
    }

    /**
     * 根据ID获取数据
     *
     * @param id
     */
    @Transactional
    @MysqlDataSource
    public User getUser(Integer id) {

        return userDao.getUserById(id);
    }

    /**
     * 检查登录名称是否重复
     *
     * @param user
     * @return
     */
    @Transactional
    @MysqlDataSource
    public Optional checkLoginNameRepeat(User user) {
        if (userDao.getUserByLoginName(user.getLoginName()) != null) {
            return Optional.of(new JsonResponse<String>(JsonResponse.RETURN_FAILURE, "自增主键类型重复，不能新增"));
        }
        return Optional.absent();
    }

    /**
     * 查询数据(主管数据)
     *
     * @param userParams
     * @param page
     */
    @Transactional
    @MysqlDataSource
    public Table queryOSUserTable(UserParams userParams, Page page) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", userParams);
        return baseDao.queryTableByPage("queryOSUser", params, page);
    }

    /**
     * 根据登录名称获取用户信息
     *
     * @param loginName
     * @return
     */
    @MysqlDataSource
    public User getUserByLoginName(String loginName) {
        return userDao.getUserByLoginName(loginName);
    }

    /**
     * 根据角色Id和机构ID获取用户信息
     *
     * @param roleId
     * @param orgId
     * @return
     */
    @MysqlDataSource
    public List getUserByRoleNameAndOrgId(String roleId, String orgId) {
        return userDao.getUserByRoleNameAndOrgId(roleId, orgId);
    }
    @MysqlDataSource
    public User getUserForLogin(String loginName){
        return  userDao.getUserForLogin(loginName);  }

}